package com.example.ndktest;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketException;

public class NetClient {
	private String ipAddr = null;
	private int port = 0;
	DatagramSocket socket;
	InetSocketAddress isa;
	InetAddress add;
	private int iPacketCounter = 0;
	
	public void setIpParam(String ip_addr, int port_num)
	{
		ipAddr = ip_addr;
		port = port_num;
	}
	
	public void startNetClient()
	{
		try {
			socket = new DatagramSocket();
			isa = new InetSocketAddress(ipAddr, port);
			add = isa.getAddress();
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void sendData(byte[] buf, int size, long ts)
	{
		try {
			DatagramPacket dp = new DatagramPacket(buf,
					size, add, port);
			socket.send(dp);
			iPacketCounter++;
			MainActivity.sendMsgToUI(String.valueOf(iPacketCounter));
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("Error 1");
		}
	}
	
	public void stopNetClient()
	{
		socket.close();
	}
}
